// pages/profile/profile.js
const app = getApp();

Page({
  data: {
    userInfo: { nickname: '', avatar: '', id: '' },
    stats: {
      totalGames: 0,
      winRate: 0,
      avgScore: 0
    },
    settings: [
      {
        id: 'notifications',
        icon: '🔔',
        title: '通知设置',
        value: true,
        type: 'switch'
      },
      {
        id: 'privacy',
        icon: '🔒',
        title: '隐私设置',
        value: '',
        type: 'arrow'
      },
      {
        id: 'about',
        icon: 'ℹ️',
        title: '关于我们',
        value: '',
        type: 'arrow'
      }
    ]
  },

  onLoad: function() {
    // 检查登录状态
    if (!app.globalData.userInfo || app.globalData.userInfo === null) {
      wx.navigateTo({ url: '/pages/login/login' });
      return;
    }
    
    this.setData({
      userInfo: app.globalData.userInfo || { nickname: '', avatar: '', id: '' }
    });
    this.loadUserStats();
  },

  onShow: function() {
    // 每次显示页面时刷新数据
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo
      });
      this.loadUserStats();
    }
  },

  // 加载用户统计数据
  loadUserStats: function() {
    const that = this;
    
    wx.request({
      url: app.globalData.apiBaseUrl + '/user/stats',
      method: 'GET',
      header: {
        'Authorization': 'Bearer ' + app.globalData.token
      },
      success: (res) => {
        if (res.data.code === 200) {
          that.setData({
            stats: res.data.data
          });
        }
      }
    });
  },

  // 处理设置项点击
  handleSettingTap: function(e) {
    const id = e.currentTarget.dataset.id;
    
    switch (id) {
      case 'privacy':
        wx.showToast({
          title: '隐私设置功能开发中',
          icon: 'none'
        });
        break;
      case 'about':
        wx.navigateTo({
          url: '/pages/about/about'
        });
        break;
    }
  },

  // 处理开关切换
  handleSwitchChange: function(e) {
    const id = e.currentTarget.dataset.id;
    const value = e.detail.value;
    
    // 更新设置项的值
    const settings = this.data.settings;
    for (let i = 0; i < settings.length; i++) {
      if (settings[i].id === id) {
        settings[i].value = value;
        break;
      }
    }
    
    this.setData({ settings });
    
    // 保存设置到服务器
    if (id === 'notifications') {
      wx.request({
        url: app.globalData.apiBaseUrl + '/user/settings',
        method: 'POST',
        header: {
          'Authorization': 'Bearer ' + app.globalData.token
        },
        data: {
          notifications: value
        }
      });
    }
  },

  // 清除缓存
  clearCache: function() {
    wx.showModal({
      title: '清除缓存',
      content: '确定要清除应用缓存吗？',
      success: (res) => {
        if (res.confirm) {
          wx.clearStorageSync();
          wx.showToast({
            title: '缓存已清除',
            icon: 'success'
          });
        }
      }
    });
  },

  // 退出登录
  logout: function() {
    wx.showModal({
      title: '退出登录',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          // 清除本地存储的登录信息
          wx.clearStorageSync();
          
          // 清除全局数据
          app.globalData.userInfo = null;
          app.globalData.token = '';
          
          // 跳转到登录页
          wx.reLaunch({
            url: '/pages/login/login'
          });
        }
      }
    });
  },

  // 打开客服
  openCustomerService: function() {
    wx.navigateTo({ url: '/pages/customer-service/customer-service' });
  }
});